﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Weapp.ControlLibrary.CustomBasicControlers;

namespace Weapp.ControlLibrary
{
    public partial class BaseField : UserControl
    {
        #region Constructors
        public BaseField()
        {
            InitializeComponent();
            heightWithoutLabel = Height - labelFieldName.Height - 1;
            FieldNameFontSize = LabelFieldName.Font.Size;
        }
        #endregion

        #region Private Members
        private System.Drawing.Color onFocusControlColor;
        private System.Drawing.Color disabledForeColor;
        private HorizontalAlignment textAlign;
        private float fieldNameFontSize;
        private int heightWithoutLabel;
        #endregion

        #region Properties
        [Description("The field name (displayed below the line)."), Category("Appearance")]
        public string FieldName
        {
            get { return labelFieldName.Text; }
            set { labelFieldName.Text = value; }
        }
        [Description("Indicates how the text should be align."), Category("Appearance")]
        public HorizontalAlignment TextAlign 
        {
            get { return textAlign; }
            set 
            {
                // bool indicating whether the new value is indeed 
                // different from the old one
                bool raiseEvent = textAlign != value;
                // assign the value to the backing field
                textAlign = value;
                // raise the event if the value has changed
                if (raiseEvent)
                {
                    BaseField_TextAlignChanged(EventArgs.Empty);
                }
            } 
        }
        [Description("The foreground color of the control when it's focused."), Category("Appearance")]
        public System.Drawing.Color OnFocusControlColor
        {
            get { return onFocusControlColor; }
            set { onFocusControlColor = value; }
        }
        [Description("The foreground color of the control's label when the control is disabled."), Category("Appearance")]
        public System.Drawing.Color DisabledForeColor
        {
            get { return disabledForeColor; }
            set { disabledForeColor = value; }
        }
        [Description("The field name label font size."), Category("Appearance")]
        public float FieldNameFontSize
        {
            get { return fieldNameFontSize; }
            set 
            {
                // bool indicating whether the new value is indeed 
                // different from the old one
                bool raiseEvent = fieldNameFontSize != value;
                // assign the value to the backing field
                fieldNameFontSize = value;
                // raise the event if the value has changed
                if (raiseEvent)
                {
                    BaseField_FieldNameFontSizeChanged(EventArgs.Empty);
                }
            }
        }
        [Browsable(false)]
        public CustomLabel LabelFieldName 
        {
            get { return labelFieldName; }  
        }
        #endregion

        #region HiddenProperties
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new BorderStyle BorderStyle { get; set; }
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override Image BackgroundImage { get; set; }
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override ImageLayout BackgroundImageLayout { get; set; }
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new ImeMode ImeMode { get; set; }
        #endregion

        #region Events
        protected virtual void UserControl1_Enter(object sender, EventArgs e)
        {
            horizontalLine.LineColor = OnFocusControlColor;
            labelFieldName.ForeColor = OnFocusControlColor;
        }
        protected virtual void UserControl1_Leave(object sender, EventArgs e)
        {
            horizontalLine.LineColor = ForeColor;
            labelFieldName.ForeColor = ForeColor;
        }
        protected virtual void BaseField_FieldNameFontSizeChanged(EventArgs e)
        {
            labelFieldName.Font = new Font(Font.FontFamily, FieldNameFontSize, Font.Style);
            labelFieldName.Height = labelFieldName.Font.Height;
            Height = heightWithoutLabel + labelFieldName.Font.Height + 1;
            horizontalLine.Top = heightWithoutLabel;
            labelFieldName.Top = heightWithoutLabel + 1;
        }
        //
        // Summary:
        //     Raises the System.Windows.Forms.TextBox.TextAlignChanged event.
        //
        // Parameters:
        //   e:
        //     An System.EventArgs that contains the event data.
        protected virtual void BaseField_TextAlignChanged(EventArgs e)
        {
            switch (TextAlign)
            {
                case HorizontalAlignment.Center:
                    labelFieldName.TextAlign = ContentAlignment.MiddleCenter;
                    break;
                case HorizontalAlignment.Left:
                    labelFieldName.TextAlign = ContentAlignment.MiddleLeft;
                    break;
                case HorizontalAlignment.Right:
                    labelFieldName.TextAlign = ContentAlignment.MiddleRight;
                    break;
                default:
                    labelFieldName.TextAlign = ContentAlignment.MiddleLeft;
                    break;
            }
        }
        protected virtual void BaseField_SizeChanged(object sender, EventArgs e)
        {
            heightWithoutLabel = Height - labelFieldName.Height - 1;
            horizontalLine.Top = heightWithoutLabel;
            horizontalLine.Size = new Size(Width, horizontalLine.LineWidth);
            labelFieldName.Top = heightWithoutLabel+1;
            labelFieldName.Width = Width;
        }
        protected virtual void BaseField_BackColorChanged(object sender, EventArgs e)
        {
            labelFieldName.BackColor = BackColor;
        }
        protected virtual void BaseField_ForeColorChanged(object sender, EventArgs e)
        {
            horizontalLine.LineColor = ForeColor;
            labelFieldName.ForeColor = ForeColor;
        }
        protected virtual void BaseField_EnabledChanged(object sender, EventArgs e)
        {
            if (Enabled)
	        {
                horizontalLine.ForeColor = ForeColor;
                labelFieldName.ForeColor = ForeColor;
	        }
            else
            {
                horizontalLine.ForeColor = DisabledForeColor;
                labelFieldName.ForeColor = DisabledForeColor;
            }
        }
        #endregion
    }
}
